package com.gongye.courseselectionsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gongye.courseselectionsystem.common.Check;
import com.gongye.courseselectionsystem.common.TimeJudge;
import com.gongye.courseselectionsystem.constants.ResultCodeEnum;
import com.gongye.courseselectionsystem.mapper.ChooseCourseMapper;
import com.gongye.courseselectionsystem.mapper.StudentMapper;
import com.gongye.courseselectionsystem.pojo.po.ChooseCourse;
import com.gongye.courseselectionsystem.pojo.po.Course;
import com.gongye.courseselectionsystem.pojo.po.Student;
import com.gongye.courseselectionsystem.pojo.vo.ResultList;
import com.gongye.courseselectionsystem.service.AddCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author baorongchuan
 */
@Slf4j
@Service
public class AddCourseServiceImpl implements AddCourseService {

    @Resource
    private ChooseCourseMapper chooseCourseMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private Check check;

    @Override
    public ResultList addCourse(Integer courseNumber, Integer studentNumber, Integer teacherJobNumber) {
        String s1 = null;
        if (!TimeJudge.time()) {
            log.info("您不在选课时间内");
            return new ResultList(ResultCodeEnum.FAILED, "您不在选课时间内");
        }
        //判断输入的编号是否都存在
        boolean idIsExist;
        try {
            idIsExist = idIsExist(courseNumber, studentNumber, teacherJobNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultList(ResultCodeEnum.FAILED, e.getMessage());
        }

        if (idIsExist) {
            //如果都存在，判断 是否已经选了2门课,
            Integer num = check.queryTwo(studentNumber);
            int a = 2;
            if (a <= num) {
                log.info("您已经选择两门课了，不能再选了");
                return new ResultList(ResultCodeEnum.FAILED, "您已经选择两门课了，不能再选了");
            }

            ChooseCourse chooseCourse = check.haveTeacher(studentNumber, teacherJobNumber);
            if (chooseCourse != null) {
                log.info("你不能重复选择同一个老师的课程");
                return new ResultList(ResultCodeEnum.FAILED, "你不能重复选择同一个老师的课程");
            }

            //不能选择同一门课
            ChooseCourse course = check.isSameCourse(studentNumber, courseNumber);
            if (course != null) {
                log.info("不能选择同一门课");
                return new ResultList(ResultCodeEnum.FAILED, "不能选择同一门课");
            }

            //判断老师的学生是否以达到三个
            boolean isThree = check.haveThree(teacherJobNumber, courseNumber);
            if (isThree) {
                log.info("该老师的课程已经被选满，请选择请他课程");
                return new ResultList(ResultCodeEnum.FAILED, "该老师的课程已经被选满，请选择请他课程");
            }

            //执行选课添加
            try {
                s1 = addChooseCourse(courseNumber, teacherJobNumber, studentNumber);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResultList(ResultCodeEnum.FAILED, e.getMessage());
            }

            //获取已选课人的名字
            ChooseCourse chooseCourse1 = check.haveTeacher(studentNumber, teacherJobNumber);
            String updateName = chooseCourse1.getUpdateName();
            String courseName = chooseCourse1.getCourseName();
            Student student = check.getStudentById(studentNumber);
            String studentEmail = student.getEmail();
            String message = "您已选课成功  姓名：[" + updateName + "]  ,课程名称:  [" + courseName + "]  ,老师编号：" + teacherJobNumber + ",学生邮箱:   " + studentEmail;
            rabbitTemplate.convertAndSend("myDirectExchange", "myDirectRoutingKey", message);
            log.info("{}同学你好，您已经选课，稍后会给你发送电子邮件", student);
        }
        return new ResultList(ResultCodeEnum.SUCCESS, s1);
    }

    /**
     * 添加课程
     * @param courseNumber 课程编号
     * @param teacherJobNumber 教师工号
     * @param studentNumber 学生学号
     * @return 添加结果
     * @throws Exception 参数异常信息
     */
    public String addChooseCourse(Integer courseNumber, Integer teacherJobNumber, Integer studentNumber) throws Exception {
        Course course = check.getCourseById(courseNumber, teacherJobNumber);

        LambdaQueryWrapper<Student> studentWrapper = new LambdaQueryWrapper<>();
        studentWrapper.eq(Student::getStudentNumber, studentNumber);
        Student student = studentMapper.selectOne(studentWrapper);

        ChooseCourse chooseCourse = new ChooseCourse();
        chooseCourse.setCourseNumber(courseNumber);
        chooseCourse.setStudentNumber(studentNumber);
        chooseCourse.setTeacherJobNumber(teacherJobNumber);
        chooseCourse.setCourseName(course.getCourseName());
        chooseCourse.setUpdateName(student.getName());
        chooseCourse.setCreateName(student.getName());
        int insert = chooseCourseMapper.insert(chooseCourse);
        if (insert == 1) {
            return "添加成功";
        }
        return "添加失败";
    }
    /**
     * 判断是参数在表中是否存在
     *
     * @param courseNumber     课程编号
     * @param studentNumber    学生学号
     * @param teacherJobNumber 老师编号
     * @return true表示存在 有一个不存在则抛出异常
     * @throws Exception 错误信息
     */
    public boolean idIsExist(Integer courseNumber, Integer studentNumber, Integer teacherJobNumber) throws Exception {
        Course course = check.getCourseById(courseNumber, teacherJobNumber);
        Student student = check.getStudentById(studentNumber);
        if (course == null) {
            throw new Exception("您输入的课程不存在");
        }
        if (student == null) {
            throw new Exception("您输入的学号不存在");
        }
        return true;
    }
}
